$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/replenish/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '姓名', name: 'name', index: 'name', width: 80 }, 			
			{ label: '手机号', name: 'phone', index: 'phone', width: 80 }, 			
			{ label: '身份证号', name: 'idcard', index: 'idcard', width: 80 }, 			
			{ label: '身份证正面图片', name: 'carduppic', index: 'carduppic', width: 80 }, 			
			{ label: '身份证反面图片', name: 'carddownpic', index: 'carddownpic', width: 80 }, 			
			{ label: '金额', name: 'money', index: 'money', width: 80 , formatter: function(value, options, row){
                return value / 100 }},
			{ label: '交易号', name: 'outorderno', index: 'outorderno', width: 80 }, 			
			{ label: '支付凭证', name: 'paypic', index: 'paypic', width: 80 }, 			
			{ label: '创建时间', name: 'createdate', index: 'createdate', width: 80 }, 			
			{ label: '更新时间', name: 'updaetdate', index: 'updaetdate', width: 80 }, 			
            { label: '状态', name: 'state', index: 'state', width: 80,formatter: function(value, options, row){
                if(value === 0){
                    return '<span class="label label-primary">未提交</span>';
                }
                if(value === 1){
                    return '<span class="label label-success">待审核</span>';
                }
                if(value === 2){
                    return '<span class="label label-warning">批准</span>';
                }
                if(value === 3){
                    return '<span class="label label-warning">拒绝</span>';
                }
            }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			phone:null
		},
		showList: true,
		title: null,
		replenish: {
            carduppic:"",
            carddownpic:"",
            paypic:""
		},
        carduppiccache:"",
        carddownpiccache:"",
        paypiccache:""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.replenish = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        approved: function(event){
			var id = vm.replenish.id;
            $.ajax({
                type: "POST",
                url: baseURL + "sys/replenish/Approved",
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
        refuse:function(event){
        	var id = vm.replenish.id;
            $.ajax({
                type: "POST",
                url: baseURL + "sys/replenish/Refuse",
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		saveOrUpdate: function (event) {
			var url = vm.replenish.id == null ? "sys/replenish/save" : "sys/replenish/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.replenish),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/replenish/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/replenish/info/"+id, function(r){
                vm.replenish = r.replenish;
                vm.carduppiccache = r.replenish.carduppic;
                vm.carddownpiccache = r.replenish.carddownpic;
                vm.paypiccache = r.replenish.paypic;
            });
		},
		reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'phone': vm.q.phone},
                page:page
            }).trigger("reloadGrid");
		}
	}
});